package jwt;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;

import java.security.interfaces.RSAPublicKey;

public class JwtVerifyExample {
    public static void main(String[] args) {
        // 读取公钥（可以从文件加载）
        RSAPublicKey publicKey = null; // 获取你的 RSA 公钥;

        // 创建验证算法对象
        Algorithm algorithm = Algorithm.RSA256(publicKey, null);

        // 创建验证器
        JWTVerifier verifier = JWT.require(algorithm)
                .withIssuer("auth0")  // 验证签发者
                .build();

        // 验证 Token
        String token = "YOUR_JWT_TOKEN_HERE";  // 替换为要验证的 JWT
        try {
            DecodedJWT jwt = verifier.verify(token);
            System.out.println("Token is valid");
            System.out.println("Subject: " + jwt.getSubject());
            System.out.println("Role: " + jwt.getClaim("role").asString());
        } catch (Exception e) {
            System.out.println("Invalid token: " + e.getMessage());
        }




    }
}
